package com.info.pello;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet class update
 * 
 * @author Carlos Rota Jimenez
 * @version 1.0
 */
@WebServlet("/Update")
public class Update extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Update() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Tale tale = selectTale(request.getParameter("id"));
		request.setAttribute("tale", tale);
		
		this.getServletContext().
		getRequestDispatcher("/update.jsp").
			forward(request,response);
	}

	/**
	 * 
	 * @param parameter
	 * @return
	 */
	private Tale selectTale(String id) {
		Tale tale = new Tale();	
		String urlcon = "jdbc:mysql://localhost:3306/onceupon";
		
		
		try {
			String allTales = "" ;
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = 
					DriverManager.getConnection(urlcon, "root","");
					Statement stat = conn.createStatement();
					ResultSet resultSet = stat.executeQuery("Select * from tale where id=" + id);
					
					if(resultSet.next()){
						tale.setId(resultSet.getInt("id"));
						tale.setTitle(resultSet.getString("title"));
						tale.setTale(resultSet.getString("tale"));
					
						
						/*allTales+= " | <a href='Delete?id="+id+"'>Delete</a>";
						allTales+= " | <a href='Update?id="+id+"'>Update</a>";
						allTales+="<br />";*/
					}
					
					resultSet.close();
					conn.close();
		} catch (Exception e) {
			
			 e.printStackTrace();			 

		}
		return tale;
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		updateTale(request.getParameter("id"),request.getParameter("title"),request.getParameter("tale"));
				
		this.getServletContext().
		getRequestDispatcher("/Home").
			forward(request,response);
	}
	/**
	 * Updates tale
	 * @param id
	 * @param title
	 * @param tale
	 */
	private void updateTale(String id, String title, String tale) {

String urlcon = "jdbc:mysql://localhost:3306/onceupon";
		
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = 	DriverManager.getConnection(urlcon, "root","");
				String updateSQL = "update tale set title=?, tale=? where id=?";
				PreparedStatement prep = conn.prepareStatement(updateSQL); 
				prep.setString(1, title);
				prep.setString(2, tale);
				prep.setString(3, id);
				prep.addBatch();
				prep.executeBatch();
					
			conn.close();
										
		} catch (Exception e) {
			
			 e.printStackTrace();
		}
		
	}

}
